<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/base}">
<head>
    <title>注册 - 暖心助农选品平台</title>
</head>
<body>
    <div layout:fragment="content">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h4 class="mb-0">农户注册</h4>
                    </div>
                    <div class="card-body">
                        <div id="errorMessage" class="alert alert-danger" style="display: none;"></div>
                        <div id="successMessage" class="alert alert-success" style="display: none;"></div>
                        
                        <form id="registerForm" th:action="@{/register}" method="post" th:object="${user}">
                            <div class="mb-3">
                                <label for="username" class="form-label">用户名</label>
                                <input type="text" class="form-control" id="username" name="username" required minlength="3" maxlength="20">
                            </div>
                            
                            <div class="mb-3">
                                <label for="password" class="form-label">密码</label>
                                <input type="password" class="form-control" id="password" name="password" required minlength="6">
                            </div>
                            
                            <div class="mb-3">
                                <label for="realName" class="form-label">真实姓名</label>
                                <input type="text" class="form-control" id="realName" name="realName" required>
                            </div>
                            
                            <div class="mb-3">
                                <label for="phone" class="form-label">联系电话</label>
                                <input type="tel" class="form-control" id="phone" name="phone" required pattern="[0-9]{11}">
                            </div>
                            
                            <div class="mb-3">
                                <label for="address" class="form-label">地址</label>
                                <input type="text" class="form-control" id="address" name="address" required>
                            </div>
                            
                            <div class="alert alert-info">
                                注册后需要等待管理员审核通过才能登录系统。
                            </div>
                            
                            <button type="submit" class="btn btn-primary">注册</button>
                            <a th:href="@{/login}" class="btn btn-link">已有账号？去登录</a>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <th:block layout:fragment="scripts">
        <script>
            document.getElementById('registerForm').addEventListener('submit', function(e) {
                e.preventDefault();
                
                const formData = new FormData(this);
                const errorDiv = document.getElementById('errorMessage');
                const successDiv = document.getElementById('successMessage');
                
                fetch(this.action, {
                    method: 'POST',
                    body: formData
                })
                .then(response => response.text())
                .then(result => {
                    if (result.includes('成功')) {
                        errorDiv.style.display = 'none';
                        successDiv.style.display = 'block';
                        successDiv.textContent = result;
                        setTimeout(() => {
                            window.location.href = '[[@{/login}]]';
                        }, 2000);
                    } else {
                        errorDiv.style.display = 'block';
                        successDiv.style.display = 'none';
                        errorDiv.textContent = result;
                    }
                })
                .catch(error => {
                    errorDiv.style.display = 'block';
                    successDiv.style.display = 'none';
                    errorDiv.textContent = '注册失败，请稍后重试';
                });
            });
        </script>
    </th:block>
</body>
</html> 